home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / convrtrs / pbmplus / update4.lha / src / pgm / pgmhisteq.c < prev   
Encoding:
C/C++ Source or Header  |  1993-07-08  |  1.7 KB  |  73 lines

  1. #ifndef indent
  2. static char rcsid[] = "$Id: pgmhisteq.c,v 1.2 1993/03/22 09:25:45 amoss Exp amoss $";
  3. #endif
  4.  
  5. /*
  6.  *  Apply histogram equalization to a PGM file.
  7.  */
  8.  
  9. /*#include <malloc.h>*/
  10. #include <stdlib.h>
  11. #include <pgm.h>
  12.  
  13. void main (ac, av)
  14.         int ac;
  15.         char **av;
  16. {
  17.         FILE *ifp;
  18.         gray maxval, **image;
  19.         register gray *gP;
  20.         double P;
  21.         int rows, cols, sum, i, j, *hist, *newhist;
  22.  
  23.         pgm_init (&ac, av);
  24.  
  25.         switch (ac) {
  26.  
  27.         case 1:
  28.                 ifp = stdin;
  29.                 break;
  30.  
  31.         case 2:
  32.                 ifp = pm_openr (av[1]);
  33.                 break;
  34.  
  35.         default:
  36.                 pm_usage ("[pgmfile]");
  37.         }
  38.  
  39.         image = pgm_readpgm (ifp, &cols, &rows, &maxval);
  40.  
  41.         pm_close (ifp);
  42.  
  43.         if (! (hist = (int *) calloc (maxval + 1, sizeof (int))))
  44.                 pm_error ("out of memory (input histogram)");
  45.  
  46.         if (! (newhist = (int *) malloc ((maxval + 1) * sizeof (int))))
  47.                 pm_error ("out of memory (output histogram)");
  48.  
  49.         /* Build histogram. */
  50.         for (i = 0; i < rows; ++i)
  51.                 for (j = 0, gP = image[i]; j < cols; ++j, ++gP)
  52.                         ++hist[(int) *gP];
  53.  
  54.         P = (double) (maxval + 1) / (double) (rows * cols);
  55.  
  56.         newhist[0] = (int) P;
  57.  
  58.         for (i = 1, sum = hist[0]; i < maxval; ++i) {
  59.                 newhist[i] = (int) (P * sum);
  60.  
  61.                 sum += hist[i];
  62.         }
  63.  
  64.         for (i = 0; i < rows; ++i)
  65.                 for (j = 0, gP = image[i]; j < cols; ++j, ++gP)
  66.                         *gP = (gray) newhist[(int) *gP];
  67.  
  68.         pgm_writepgm (stdout, image, cols, rows, maxval, 0);
  69.  
  70.         exit (0);
  71. }
  72.  
  73.